﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>部门新增/编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm">
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>id</label>
            <div class="layui-input-block">
                <input type="text" id="ipt_id" name="id" lay-verify="required|number"  required autocomplete="off" placeholder="id"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>名称</label>
            <div class="layui-input-block">
                <input type="text" id="ipt_name" name="name" lay-verify="required" required placeholder="名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label">上级</label>
            <div class="layui-input-block">
                <select id="ddl_parentId" name="parentId">
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" placeholder="地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">邮件地址</label>
            <div class="layui-input-block">
                <input type="text" name="mail" lay-verify="mailnull" placeholder="电子邮箱地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="tel" name="phone" lay-verify="phonenull" placeholder="联系手机号码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>排序</label>
            <div class="layui-input-block">
                <input type="text" name="rank" lay-verify="number" placeholder="联系排序序号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    });
    layui.use(['jquery','form','validate','common'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            validate = layui.validate;
        common = layui.common;
        validate.regVerify();//注册验证
        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");

        common.initSelectData("#ddl_parentId","./common/deptTree4Select");
        if (type=="edit"){
            //加载数据
            $("#ipt_id").attr("readonly","readonly");

            //获取用户信息
            $.ajax({
                url: "dept/info",
                dataType: "json",
                data: { id: id },
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        var data = rtn.data;
                        form.val('dataForm',{
                            id:data.id,
                            name:data.name,
                            parentId:data.parentId,
                            rank:data.rank,
                            address:data.address,
                            mail:data.mail,
                            phone:data.phone,
                        });
                    }
                }
            });
        }

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //common.consoleDebug(JSON.stringify(data.field));
            $.ajax({
                type: 'post',
                url: 'dept/'+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {
                        common.showInfo('处理成功!',function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>